package com.sddz.pacs.aop;

/**
 * @Author SunLi
 * @Date 2018/9/18
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sddz.pacs.annotation.SysLogger;
import com.sddz.pacs.dto.JsonResult;
import com.sddz.pacs.entity.HealthUser;
import com.sddz.pacs.entity.SysLog;
import com.sddz.pacs.entity.SysUser;
import com.sddz.pacs.service.SysLogService;
import com.sddz.pacs.support.Constant;
import com.sddz.pacs.util.HttpContextUtils;
import com.sddz.pacs.util.IPUtils;
import com.sddz.pacs.util.ShiroUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * 类SysLogAspect的功能描述:
 * 系统日志，切面处理类
 */
@Aspect
@Component
public class SysLogAspect {

    //此处注入日志持久化服务对象
    @Autowired
    private SysLogService sysLogService;


    @Pointcut("@annotation(com.sddz.pacs.annotation.SysLogger)")
    public void logPointCut() {

    }

    @AfterReturning(pointcut = "logPointCut()", returning = "keys")
    public void doAfterReturningAdvice1(JoinPoint joinPoint, Object keys) {
        //获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLog sysLog = new SysLog();

        SysLogger sysLogger = method.getAnnotation(SysLogger.class);
        if (sysLogger != null) {
            //操作业务方法名称
            sysLog.setOperation(sysLogger.value());
            //操作业务类型
            sysLog.setOperateType(sysLogger.type());
        }

        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");

        //请求的参数
        Object[] args = joinPoint.getArgs();
        if (args.length > 0) {

            String params = JSON.toJSONString(args[0]);
            if (methodName.equals("authorize")) {
                sysLog.setParams("图片文件");
            } else {
                sysLog.setParams(params);
            }


            //设置IP地址
            sysLog.setIp(IPUtils.getIpAddr(request));
            //存储操作用户
            SysUser sysUser = ShiroUtils.getUserEntity();
            if (sysUser != null ) {

                if (sysUser.getState().equals("00")){
                    sysLog.setUserName(sysUser.getUserName());
                    sysLog.setUserId(sysUser.getId());
                    sysLog.setState(Constant.XING_TAI_SHI_STATE);
                }else if (sysUser.getState().equals("0")){
                    sysLog.setUserName(sysUser.getUserName());
                    sysLog.setUserId(sysUser.getId());
                    sysLog.setState(Constant.QIAO_XI_STATE);
                } else{
                    sysLog.setUserName(sysUser.getUserName());
                    sysLog.setUserId(sysUser.getId());
                    sysLog.setState(Constant.QIAO_DONG_STATE);
                }
            }
            //从session 中获取居民的个人信息
            HealthUser healthUser =(HealthUser) request.getSession().getAttribute("healthUser");
            if (healthUser != null){

                if (healthUser.getState().equals("0")){
                    sysLog.setUserName(healthUser.getName());
                    sysLog.setUserId(healthUser.getId());
                    sysLog.setState(Constant.QIAO_XI_STATE);
                }else{
                    sysLog.setUserName(healthUser.getName());
                    sysLog.setUserId(healthUser.getId());
                    sysLog.setState(Constant.QIAO_DONG_STATE);
                }
            }
            //存储返回结果
            if (keys instanceof JsonResult) {
                JsonResult jsonResult = (JsonResult) keys;
                sysLog.setRemark(jsonResult.getMsg());
                //获取返回结果
                sysLog.setOperateResult(jsonResult.getCode());
            }
            //持久化操作日志
            sysLogService.insert(sysLog);
        }
    }
}
